<p>
	<strong>Calendar Week Management</strong><br />
	Here you can add new weeks to the calendar and you can edit weeks that are already there. <br>Click the "Add new week" button for adding a new week, or select an option at the right of an existing week.</p>
	<span class="nextWeekTable">
		<form method="POST">
		<p><table>
		<tr><td><br>Weeks</td><td>House <br>Weekday</td><td>House <br>Weekend</td><td><br>Family Room</td><td colspan="2"><br>Status (options)</td></tr>
			<?php
			$result=get_all_dbWeeks();
			for($i=0;$i<mysql_num_rows($result);++$i){
				$week=mysql_fetch_row($result);
                if ($i==mysql_num_rows($result)-1 || $i==0 && $week[5]=="archived")
                   $remove = true;
                else $remove = false;
				if(($week[5]=="archived" && $_GET['archive']=="true") || $week[5]=="published" || ($week[5]=="unpublished" && $_SESSION['access_level']>=2))
					echo ("<tr><td>".$week[6]."</td><td>Group ".$week[2]."</td>" .
							"<td>Group ".$week[3]."</td><td>Group ".$week[4]."</td><td colspan=\"2\">".show_week_options($week, $remove) . "</td></tr>");
			}
			echo ('<tr> <td colspan="4">');
			// finds the parameters for "generate next week" button
				if($_SESSION['access_level']>=2) {
					// finds the last week, and calculates next week's groups
					$last=$week[0];
					$weekday_group=$week[2]+1;
					if($weekday_group>2)
						$weekday_group=$weekday_group-2;
					$weekend_group=$week[3]+1;
					if($weekend_group==5){
						$prev_week_month=date("m",mktime(0,0,0,substr($last,0,2),substr($last,3,2)+5,substr($last,6,2)));
						$new_week_month=date("m",mktime(0,0,0,substr($last,0,2),substr($last,3,2)+12,substr($last,6,2)));
						if($new_week_month!=$prev_week_month)
							$weekend_group=1;
					}
					if($weekend_group>5)
							$weekend_group=$weekend_group-5;
					$family_room_group=$week[4]+1;
					if($family_room_group>2)
						$family_room_group=$family_room_group-2;
					$new_week_timestamp = mktime(0,0,0,substr($last,0,2),substr($last,3,2)+7,substr($last,6,2));
					echo "<input type=\"hidden\" name=\"_new_week_timestamp\" value=\"".$new_week_timestamp."\">
								<input type=\"hidden\" name=\"_submit_check_newweek\" value=\"1\">
								<input type=\"submit\" value=\"Add new week\" name=\"Add new week\">";
            if ($firstweek) {
					echo ('&nbsp;&nbsp;   Month <select name="month">');
			$months = array('SELECT','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
			$today=date("M");
			for($i=0;$i<=12;++$i) {
				echo ("<option value=\"".$i."\"");
				if($_POST['month']==$i || $today==$months[$i])
					echo (" SELECTED");
				echo (">".$months[$i]."</option>");
			}
			echo ('</select> Day <select name="day"> <option value="0">SELECT</option>');
			$today=date("j");
			for($i=1;$i<=31;++$i) {
				echo ("<option ");
				if($_POST['day']==$i || $today==$i)
					echo (" SELECTED");
				echo (">".$i."</option>");
			}
			echo ('</select> Year <select name="year"> <option value="0">SELECT</option>');
			$i=date("Y");
			echo ("<option ");
			if($_POST['year']==$i || $_POST['year']!=$i+1)
					echo (" SELECTED");
			echo (">".$i."</option>");
			echo ("<option ");
			if($_POST['year']==$i+1)
					echo (" SELECTED");
			echo (">".($i+1)."</option>");
			echo ("</select>");
       }
					echo "<br> &nbsp; with House Weekday Group <select name=\"weekday_group\">";
					// the group option menus, expected groups selected by default
					for($i=1;$i<=2;++$i) {
						echo "<option value=\"".$i."\"";
						if($i==$weekday_group)
							echo " SELECTED";
						echo ">".$i."</option>";
					}
					echo "</select>, House Weekend Group <select name=\"weekend_group\">";
					for($i=1;$i<=5;++$i) {
						echo "<option value=\"".$i."\"";
						if($i==$weekend_group)
							echo " SELECTED";
						echo ">".$i."</option>";
					}
					echo "</select><br> &nbsp; and Family Room Group <select name=\"family_room_group\">";
					for($i=1;$i<=2;++$i) {
						echo "<option value=\"".$i."\"";
						if($i==$family_room_group)
							echo " SELECTED";
						echo ">".$i."</option>";
					}
					echo "</select>";
				}
				else
					echo "&nbsp;</td>";

            echo('<td align="center"><a href="addWeek.php?archive=');
			if($_GET['archive']=="true")
					echo "false";
			else
					echo "true";
			echo('">');
			if($_GET['archive']=="true")
					echo "Hide";
			else
					echo "View";
			echo(' Archive</a></td>');

            // determines what options apply to each week
			function show_week_options($week, $remove) {
				$id=$week[0];
				$status=$week[5];
				$end=$week[7];
				$options="";
				if($status=="archived") {
					$options="archived (<a href=\"calendar.php?id=".$week[0]."\">view</a>)";
					if ($remove)
					   $options = $options . " (<a href=\"addWeek.php?remove=".$week[0]."\">remove</a>)";
				}
				else if (time()>$end) {
					$options="archived (<a href=\"calendar.php?id=".$week[0]."\">view</a>)";
					$week2=get_dbWeeks($week[0]);
					$week2->set_status("archived");
					update_dbWeeks($week2);
				}
				else if ($status=="unpublished" && $_SESSION['access_level']>=2) {
					$options="unpublished (<a href=\"addWeek.php?reset=".$week[0]."\">reset</a>)
							(<a href=\"calendar.php?id=".$week[0]."&edit=true\">edit</a>)
							(<a href=\"addWeek.php?publish=".$week[0]."\">publish</a>)
							(<a href=\"calendar.php?id=".$week[0]."\">view</a>)";
					if ($remove)
					   $options = $options . " (<a href=\"addWeek.php?remove=".$week[0]."\">remove</a>)";
				}
				else if ($status=="published" && $_SESSION['access_level']>=2)
					$options="published (<a href=\"calendar.php?id=".$week[0]."&edit=true\">edit</a>)
							(<a href=\"addWeek.php?publish=".$week[0]."\">unpublish</a>)
							(<a href=\"calendar.php?id=".$week[0]."\">view</a>)";
                else if ($status=="published")
					$options="published (<a href=\"calendar.php?id=".$week[0]."&edit=true\">edit</a>)
							(<a href=\"calendar.php?id=".$week[0]."\">view</a>)";
				return $options;
			}
			?>
			</tr>
		</table></p>
		</form></span>
